//
//  WPFilterCell.m
//  PhnixHome
//
//  Created by Levy Xu on 2018/3/7.
//  Copyright © 2018年 Levy Xu. All rights reserved.
//

#import "WPFilterCell.h"

@interface WPFilterCell ()
@property(nonatomic, strong) QMUIFillButton *buyBtn;
@property(nonatomic, strong) UIImageView *leftArrowIV;
@end

@implementation WPFilterCell

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = UIColorClear;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.textLabel.textColor = UIColorWhite;
        self.textLabel.adjustsFontSizeToFitWidth = YES;
        self.detailTextLabel.textColor = UIColorGray9;
        self.detailTextLabel.adjustsFontSizeToFitWidth = YES;
        self.textLabel.font = KKFontMake(16);
        self.detailTextLabel.font = KKFontMake(14);
        self.qmui_borderPosition = QMUIBorderViewPositionTop;
        
        _buyBtn = [QMUIFillButton buttonWithType:UIButtonTypeCustom];
        _buyBtn.fillColor = UIColorWhite;
        _buyBtn.titleLabel.font = CodeFontMake(14);
        _buyBtn.titleTextColor = UIColorMakeWithHex(@"4898fa");
        [_buyBtn setTitle:NSLocalizedString(@"btn_buy", nil) forState:UIControlStateNormal];
        [_buyBtn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_buyBtn];
        
        _leftArrowIV = [[UIImageView alloc] initWithImage:UIImageMake(@"water_arrow_left")];
        [self.contentView addSubview:_leftArrowIV];
    }
    return self;
}

-(void)clicked:(id)sender {
    [_delegate buyClicked:self];
}

-(void)setLifeTime:(NSInteger)lifeTime {
    _lifeTime = lifeTime;
    [self setNeedsLayout];
}

-(void)setUsedTime:(NSInteger)usedTime {
    _usedTime = usedTime;
    [self setNeedsLayout];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    
    [self setNeedsLayout];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    NSInteger leftTime = MAX(_lifeTime - _usedTime, 0);
    self.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"tips_left_days", nil), leftTime / 24];
    
    _shouldBuy = leftTime < _lifeTime * 0.1;
    
    CGFloat imageHeight = 40.0f;
    CGFloat buyHeight = 36.0f;
    CGFloat labelHeight = 20.0f;
    if (self.qmui_height < 40) {
        imageHeight = self.qmui_height - 2.0f;
        buyHeight = 26.0f;
        labelHeight = self.qmui_height / 2.0f - 1.0f;
    }
    
    self.imageView.frame = CGRectMake(15, (self.qmui_height - imageHeight) / 2, imageHeight, imageHeight);
    
    if (_shouldBuy) {
        self.textLabel.frame = CGRectMake(60, self.qmui_height / 2.0 - labelHeight, self.qmui_width - 145, labelHeight);
        self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        self.detailTextLabel.frame = CGRectMake(60, self.qmui_height / 2.0, self.qmui_width - 145, labelHeight);
        self.buyBtn.hidden = NO;
        self.buyBtn.frame = CGRectMake(SCREEN_WIDTH - 94, (self.qmui_height - buyHeight) / 2, 70, buyHeight);
        self.leftArrowIV.hidden = self.isEditing;
        self.leftArrowIV.frame = CGRectMake(SCREEN_WIDTH - 22, (self.qmui_height - 20) / 2, 20, 20);
    } else {
        self.textLabel.frame = CGRectMake(60, self.qmui_height  / 2 - 10, self.qmui_width / 2 - 35, 20);
        self.detailTextLabel.textAlignment = NSTextAlignmentRight;
        self.detailTextLabel.frame = CGRectMake(self.qmui_width / 2 + 25, self.qmui_height / 2 - 10, self.qmui_width / 2 - 35, 20);
        self.buyBtn.hidden = YES;
        self.leftArrowIV.hidden = YES;
    }
}

@end
